//Player request.
const kPlayVideoReq         = 0;
const kPauseVideoReq        = 1;
const kStopVideoReq         = 2;

//Player response.
const kPlayVideoRsp         = 0;
const kAudioInfo            = 1;
const kVideoInfo            = 2;
const kAudioData            = 3;
const kVideoData            = 4;

//Downloader request.
const kGetFileInfoReq       = 0;
const kDownloadFileReq      = 1;
const kCloseDownloaderReq   = 2;

//Downloader response.
const kGetFileInfoRsp       = 0;
const kFileData             = 1;

//Downloader Protocol.
const kProtoHttp            = 0;
const kProtoWebsocket       = 1;

//Decoder request.
const kInitDecoderReq       = 0;
const kUninitDecoderReq     = 1;
const kOpenDecoderReq       = 2;
const kCloseDecoderReq      = 3;
const kFeedDataReq          = 4;
const kStartDecodingReq     = 5;
const kPauseDecodingReq     = 6;
const kSeekToReq            = 7;

//Decoder response.
const kInitDecoderRsp       = 0;
const kUninitDecoderRsp     = 1;
const kOpenDecoderRsp       = 2;
const kCloseDecoderRsp      = 3;
const kVideoFrame           = 4;
const kAudioFrame           = 5;
const kStartDecodingRsp     = 6;
const kPauseDecodingRsp     = 7;
const kDecodeFinishedEvt    = 8;
const kRequestDataEvt       = 9;
const kSeekToRsp            = 10;

const level_all             = 0
const level_debug           = 1;
const level_info            = 2;
const level_warn            = 3;
const level_error           = 4;

function Logger(module) {
    this.module = module;
    this.level = level_debug;
    this.show_time = 0;
}

Logger.prototype.log = function (line) {
    if (this.level <= level_all) {
        console.log(this.currentTimeStr() + "[" + this.module + "]" + line);
    }
}

Logger.prototype.logError = function (line) {
    if (this.level <= level_error) {        
        console.log(this.currentTimeStr() + "[" + this.module + "][ER] " + line);
    }
}

Logger.prototype.logInfo = function (line) {
    if (this.level <= level_info) {
        console.log(this.currentTimeStr() + "[" + this.module + "][IF] " + line);
    }
}

Logger.prototype.logDebug = function (line) {
    if (this.level <= level_debug) {
        console.log(this.currentTimeStr() + "[" + this.module + "][DT] " + line);
    }
}

Logger.prototype.currentTimeStr = function () {
    if(this.show_time) {
        var now = new Date(Date.now());
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        var hour = now.getHours();
        var min = now.getMinutes();
        var sec = now.getSeconds();
        var ms = now.getMilliseconds();
        return "[" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + ":" + ms + "]";
    } else {
        return "";
    }
}

function appendByteArray(buffer1, buffer2) {
    let tmp = new Uint8Array((buffer1.byteLength|0) + (buffer2.byteLength|0));
    tmp.set(buffer1, 0);
    tmp.set(buffer2, buffer1.byteLength|0);
    return tmp;
}
